package com.ruoyi.web.controller.gas;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.gas.domain.GasEvent;
import com.ruoyi.gas.domain.GasWarnRectify;
import com.ruoyi.gas.domain.vo.DeviceData;
import com.ruoyi.gas.domain.vo.EventData;
import com.ruoyi.gas.domain.vo.SafeVo;
import com.ruoyi.gas.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.gas.domain.GasEventDetail;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 安全事件详情Controller
 *
 * @author ruoyi
 * @date 2023-12-11
 */
@Slf4j
@RestController
@RequestMapping("/gas/detail")
public class GasEventDetailController extends BaseController
{
    @Autowired
    private IGasEventDetailService gasEventDetailService;

    @Autowired
    private IGasEventService gasEventService;

    @Autowired
    private IGasEventDictService gasEventDictService;

    @Autowired
    private IGasDetectionObjectDictService gasDetectionObjectDictService;

    @Autowired
    private IGasDataUnitDictService gasDataUnitDictService;

    @Autowired
    private IGasWarnRectifyService gasWarnRectifyService;

    /**
     * 查询安全事件详情列表
     */
    @PreAuthorize("@ss.hasPermi('gas:detail:list')")
    @GetMapping("/list")
    public TableDataInfo list(GasEventDetail gasEventDetail)
    {
        startPage();
        List<GasEventDetail> list = gasEventDetailService.selectGasEventDetailList(gasEventDetail);
        return getDataTable(list);
    }

    /**
     * 导出安全事件详情列表
     */
    @PreAuthorize("@ss.hasPermi('gas:detail:export')")
    @Log(title = "安全事件详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GasEventDetail gasEventDetail)
    {
        List<GasEventDetail> list = gasEventDetailService.selectGasEventDetailList(gasEventDetail);
        ExcelUtil<GasEventDetail> util = new ExcelUtil<GasEventDetail>(GasEventDetail.class);
        util.exportExcel(response, list, "安全事件详情数据");
    }

    /**
     * 获取安全事件详情详细信息
     */
    @PreAuthorize("@ss.hasPermi('gas:detail:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(gasEventDetailService.selectGasEventDetailById(id));
    }

    /**
     * 新增安全事件详情
     */
    @PreAuthorize("@ss.hasPermi('gas:detail:add')")
    @Log(title = "安全事件详情", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GasEventDetail gasEventDetail)
    {
        return toAjax(gasEventDetailService.insertGasEventDetail(gasEventDetail));
    }

    /**
     * 修改安全事件详情
     */
    @PreAuthorize("@ss.hasPermi('gas:detail:edit')")
    @Log(title = "安全事件详情", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GasEventDetail gasEventDetail)
    {
        return toAjax(gasEventDetailService.updateGasEventDetail(gasEventDetail));
    }

    /**
     * 删除安全事件详情
     */
    @PreAuthorize("@ss.hasPermi('gas:detail:remove')")
    @Log(title = "安全事件详情", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(gasEventDetailService.deleteGasEventDetailByIds(ids));
    }


    /**
     * ZYJ
     * 发生安全事件时，会推送该事件数据
     * @param safeVo
     * @return
     */
    @PostMapping("/occurSafeEvent")
    public AjaxResult occurSafeEvent(@RequestBody SafeVo safeVo){
        log.info("安全事件接收数据:"+safeVo);
        //事件对象
        GasEvent gasEvent=new GasEvent();
        gasEvent.setEventTime(safeVo.getEventTime());
        gasEvent.setEventCode(safeVo.getEventCode());
        gasEvent.setCreateTime(DateUtils.getNowDate());
        int eventFlag = gasEventService.addGasEvent(gasEvent);
        if (eventFlag>0){
            //事件数据信息
            List<EventData> eventDataList = safeVo.getEventData();
            if (eventDataList!=null&&eventDataList.size()>0){
                for (EventData eventList:eventDataList) {
                    //事件详情对象
                    GasEventDetail gasEventDetail=new GasEventDetail();
                    gasEventDetail.setEventId(gasEvent.getFileId());
                    gasEventDetail.setDeviceId(eventList.getDeviceId());
                    if (eventList.getDeviceData()!=null&&eventList.getDeviceData().size()>0){
                        //设备数据信息
                        List<DeviceData> deviceDataList = eventList.getDeviceData();
                        for (DeviceData deviceList:deviceDataList) {
                            gasEventDetail.setConcentration(deviceList.getConcentration());
                            gasEventDetail.setDataUnit(deviceList.getDataUnit());
                            gasEventDetail.setMonitorObject(deviceList.getMonitorObject());
                            gasEventDetail.setChannelCode(deviceList.getChannelCode());
                            gasEventDetailService.insertGasEventDetail(gasEventDetail);

                            //隐患汇总
                            GasWarnRectify rectify=new GasWarnRectify();
                            rectify.setWarnTime(new Date(Long.parseLong(safeVo.getEventTime())));
                            rectify.setDeviceId(eventList.getDeviceId());
                            rectify.setWarnType(gasEventDictService.getNameByCode(safeVo.getEventCode()));
                            rectify.setDetection(gasDetectionObjectDictService.getNameByCode(deviceList.getMonitorObject()));
                            rectify.setUnit(gasDataUnitDictService.getNameByCode(deviceList.getDataUnit()));
                            rectify.setConcentration(deviceList.getConcentration());
                            rectify.setRoute(deviceList.getChannelCode());
                            gasWarnRectifyService.insertGasWarnRectify(rectify);
                        }
                    }
                }
            }
            return new AjaxResult(HttpStatus.SUCCESS, "成功接收数据");
        }else {
            return new AjaxResult(400, "数据接收失败");
        }
    }
}
